<template>
  <div>
    <a-card :bordered="false" class="ant-pro-components-tag-select">
      <a-form :form="form" layout="inline">
        <standard-form-row title="评价类型" block style="padding-bottom: 11px;">
          <a-form-item>
            <tag-select>
              <tag-select-option value="all">全部评价</tag-select-option>
              <tag-select-option value="positive">好评</tag-select-option>
              <tag-select-option value="neutral">中评</tag-select-option>
              <tag-select-option value="negative">差评</tag-select-option>
              <tag-select-option value="delivery">配送体验</tag-select-option>
              <tag-select-option value="app">应用体验</tag-select-option>
              <tag-select-option value="customer_service">客服体验</tag-select-option>
            </tag-select>
          </a-form-item>
        </standard-form-row>

        <standard-form-row title="配送员" grid>
          <a-row>
            <a-col :md="24">
              <a-form-item :wrapper-col="{ span: 24 }">
                <a-select
                  style="max-width: 268px; width: 100%;"
                  mode="multiple"
                  placeholder="选择配送员"
                  v-decorator="['deliveryStaff']"
                  @change="handleChange"
                >
                  <a-select-option v-for="item in deliveryStaff" :key="item.id">{{ item.name }}</a-select-option>
                </a-select>
                <a class="list-articles-trigger" @click="setRecentDelivery">只看最近配送</a>
              </a-form-item>
            </a-col>
          </a-row>
        </standard-form-row>

        <standard-form-row title="其它筛选" grid last>
          <a-row :gutter="16">
            <a-col :xs="24" :sm="24" :md="12" :lg="10" :xl="8">
              <a-form-item label="评分范围" :wrapper-col="{ xs: 24, sm: 24, md: 12 }">
                <a-select placeholder="不限" style="max-width: 200px; width: 100%;" v-model="queryParam.ratingRange">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option value="5">5星</a-select-option>
                  <a-select-option value="4">4星及以上</a-select-option>
                  <a-select-option value="3">3星及以上</a-select-option>
                  <a-select-option value="2">2星及以下</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xs="24" :sm="24" :md="12" :lg="10" :xl="8">
              <a-form-item label="评价时间" :wrapper-col="{ xs: 24, sm: 24, md: 12 }">
                <a-select placeholder="不限" style="max-width: 200px; width: 100%;" v-model="queryParam.timeRange">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option value="week">最近一周</a-select-option>
                  <a-select-option value="month">最近一个月</a-select-option>
                  <a-select-option value="quarter">最近三个月</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xs="24" :sm="24" :md="12" :lg="4" :xl="8">
              <a-button type="primary" @click="handleSearch">查询</a-button>
              <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
            </a-col>
          </a-row>
        </standard-form-row>
      </a-form>
    </a-card>

    <a-card style="margin-top: 24px;" :bordered="false">
      <a-list
        size="large"
        rowKey="id"
        :loading="loading"
        itemLayout="vertical"
        :dataSource="data"
      >
        <a-list-item :key="item.id" slot="renderItem" slot-scope="item">
          <template slot="actions">
            <icon-text type="like-o" :text="item.likes" />
            <icon-text type="message" :text="item.comments" />
            <icon-text type="clock-circle-o" :text="item.timeAgo" />
          </template>
          <a-list-item-meta>
            <a slot="title" href="javascript:void(0)">订单 #{{ item.orderSn }}</a>
            <template slot="description">
              <span>
                <a-rate :value="item.rating" disabled allow-half />
                <a-tag :color="getRatingColor(item.rating)">{{ getRatingText(item.rating) }}</a-tag>
                <a-tag v-for="tag in item.tags" :key="tag">{{ tag }}</a-tag>
              </span>
            </template>
          </a-list-item-meta>
          <article-list-content
            :description="item.content"
            :owner="item.userName"
            :avatar="item.userAvatar"
            :updateAt="item.createdAt"
          />
          <div v-if="item.reply" class="reply-content">
            <p><strong>官方回复：</strong>{{ item.reply }}</p>
            <p class="reply-time">{{ item.replyTime }}</p>
          </div>
        </a-list-item>
        <div slot="footer" v-if="data.length > 0" style="text-align: center; margin-top: 16px;">
          <a-button @click="loadMore" :loading="loadingMore">加载更多</a-button>
        </div>
      </a-list>
    </a-card>
  </div>
</template>

<script>
import { TagSelect, StandardFormRow, ArticleListContent } from '@/components'
import IconText from './components/IconText'
import moment from 'moment'
const TagSelectOption = TagSelect.Option

const deliveryStaff = [
  {
    id: 'all',
    name: '全部配送员'
  },
  {
    id: 'zhang',
    name: '张师傅'
  },
  {
    id: 'li',
    name: '李师傅'
  },
  {
    id: 'wang',
    name: '王师傅'
  },
  {
    id: 'zhao',
    name: '赵师傅'
  }
]

export default {
  name: 'OrderReviews',
  components: {
    TagSelect,
    TagSelectOption,
    StandardFormRow,
    ArticleListContent,
    IconText
  },
  data () {
    return {
      deliveryStaff,
      loading: true,
      loadingMore: false,
      data: [],
      form: this.$form.createForm(this),
      queryParam: {
        ratingRange: '',
        timeRange: '',
        reviewType: [],
        deliveryStaff: []
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    handleChange (value) {
      this.queryParam.deliveryStaff = value
    },
    getList () {
      // 模拟API请求
      setTimeout(() => {
        this.data = this.generateMockData(8)
        this.loading = false
      }, 500)
    },
    loadMore () {
      this.loadingMore = true
      // 模拟API请求
      setTimeout(() => {
        this.data = this.data.concat(this.generateMockData(5))
        this.loadingMore = false
      }, 500)
    },
    setRecentDelivery () {
      const { form: { setFieldsValue } } = this
      setFieldsValue({
        deliveryStaff: ['all']
      })
      this.queryParam.timeRange = 'week'
      this.handleSearch()
    },
    handleSearch () {
      this.loading = true
      // 模拟API请求
      setTimeout(() => {
        this.data = this.generateMockData(5)
        this.loading = false
      }, 500)
    },
    handleReset () {
      this.form.resetFields()
      this.queryParam = {
        ratingRange: '',
        timeRange: '',
        reviewType: [],
        deliveryStaff: []
      }
      this.handleSearch()
    },
    getRatingColor (rating) {
      if (rating >= 4) return 'green'
      if (rating >= 3) return 'blue'
      if (rating >= 2) return 'orange'
      return 'red'
    },
    getRatingText (rating) {
      if (rating >= 4.5) return '非常满意'
      if (rating >= 4) return '满意'
      if (rating >= 3) return '一般'
      if (rating >= 2) return '不满意'
      return '非常不满意'
    },
    generateMockData (count) {
      const tags = ['准时送达', '无人机稳定', '包装完好', '服务态度好', '配送速度快', '应用体验好']
      return Array(count).fill().map((_, i) => {
        const id = this.data.length + i + 1
        const rating = Math.floor(Math.random() * 5) + Math.random() > 0.5 ? 0.5 : 0
        const hasReply = Math.random() > 0.3
        const daysAgo = Math.floor(Math.random() * 30)

        return {
          id,
          orderSn: `ORD${String(10000 + id).padStart(5, '0')}`,
          userId: Math.floor(Math.random() * 1000) + 1,
          userName: `用户${Math.floor(Math.random() * 1000) + 1}`,
          userAvatar: `https://randomuser.me/api/portraits/${Math.random() > 0.5 ? 'men' : 'women'}/${Math.floor(Math.random() * 100)}.jpg`,
          rating,
          content: `这次配送体验${rating >= 4 ? '非常好' : rating >= 3 ? '还不错' : '有待改进'}。${
            rating >= 4
              ? '无人机准时到达，包装完好，非常满意。'
              : rating >= 3
                ? '无人机送达及时，但降落地点有点偏。'
                : '无人机晚点较多，且包装有轻微损坏。'
          }${Math.random() > 0.5 ? '希望下次继续保持。' : '期待服务能更好。'}`,
          tags: Array(Math.floor(Math.random() * 3) + 1)
            .fill()
            .map(() => tags[Math.floor(Math.random() * tags.length)]),
          likes: Math.floor(Math.random() * 100),
          comments: Math.floor(Math.random() * 20),
          createdAt: moment().subtract(daysAgo, 'days').format('YYYY-MM-DD HH:mm:ss'),
          timeAgo: `${daysAgo}天前`,
          reply: hasReply ? `感谢您的评价，我们${rating >= 3 ? '会继续保持良好的服务' : '已记录您的反馈，会积极改进'}。如有其他问题，请随时联系客服。` : null,
          replyTime: hasReply ? moment().subtract(Math.floor(Math.random() * daysAgo), 'days').format('YYYY-MM-DD HH:mm:ss') : null,
          droneId: Math.floor(Math.random() * 100) + 1,
          deliveryStaffId: ['zhang', 'li', 'wang', 'zhao'][Math.floor(Math.random() * 4)]
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-components-tag-select {
  :deep(.ant-pro-tag-select .ant-tag) {
    margin-right: 24px;
    padding: 0 8px;
    font-size: 14px;
  }
}

.list-articles-trigger {
  margin-left: 12px;
}

.reply-content {
  background-color: #f5f5f5;
  padding: 12px;
  margin-top: 12px;
  border-radius: 4px;

  .reply-time {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.45);
    margin-top: 8px;
    margin-bottom: 0;
  }
}
</style>
